/**
    [Filename]
    Purpose: [Purpose]

    @author Jason Wojcik
    @version [version]
*/

#include "CApp.h"



/*
Checks the gameboard for a row of 4 black tiles
@params t[0] lower y, t[1] upper y, t[2] lower x, t[3] upper X, t[4] type
@params type 0 - horizontal, type 2 - vertical, type 3 - diagR type 4 - diagL
@returns true (match) or false (no match)
*/
bool Check(int testArray[5], int &winColor){

    for(int y = testArray[0]; y < testArray[1]; y++){
        for(int x = testArray[2]; x < testArray[3]; x++){
            int ID = x + y * MAP_WIDTH;
            int test = 0;
            for(int k = 0; k < 4 ; k++){
                switch(testArray[4]){
                    case 0:
                        test += CArea::AreaControl.GetTileColor(ID+k);
                        break;
                    case 1:
                        test += CArea::AreaControl.GetTileColor(ID+k*MAP_WIDTH);
                        break;
                    case 2:
                        test += CArea::AreaControl.GetTileColor(ID + k - MAP_WIDTH*k);
                        break;
                    case 3:
                        test += CArea::AreaControl.GetTileColor(ID - k - MAP_WIDTH*k);
                        break;
                    default:
                        break;
                }
            }
            // black chips: '3' - 4 = 12, red chips: '5' - 4 = 20
            if (test == RED * 4 || test ==  BLACK * 4) {
                    winColor = test;
                    return true;
            }
        }
    }
    return false;
}

/*
Check to see if there are 4
@params
@return true - game won or false - game still going
*/
int CApp::WinGame(){

    //horizontal bounding box, type = 0
    int hor[] = {1,8,2,5,0};
    //vertical bounding box, type = 1
    int vert[] = {1,4,2,9,1};
    //diagonal right bounding box, type = 2;
    int diagR[] = {4,7,2,5,2};
    //diagonal left bounding box, type = 3;
    int diagL[] = {4,7,5,9,3};

    int winColor = 0;

    //check all directions for 4 matching squares

    if(Check(hor, winColor) || Check(vert, winColor) || Check(diagR, winColor) || Check(diagL, winColor)){
        SDL_Delay(250);
        return winColor;
    }
    return 0;
}

void CApp::OnLoop(){

    CFPS::FPSControl.OnLoop();

    //call the on loop function for all entities
    for(int i = 0;i < CEntity::EntityList.size();i++) {
        if(!CEntity::EntityList[i]) continue;

    CEntity::EntityList[i]->OnLoop();
    }

    //update the location of the arrow
    arrow.X = chips[turn].X + 8;

    //Collision Events
    for(int i = 0;i < CEntityCol::EntityColList.size();i++) {
        CEntity* EntityA = CEntityCol::EntityColList[i].EntityA;
        CEntity* EntityB = CEntityCol::EntityColList[i].EntityB;

        if(EntityA == NULL || EntityB == NULL) continue;

        if(EntityA->OnCollision(EntityB)) {
            EntityB->OnCollision(EntityA);
        }
    }

    CEntityCol::EntityColList.clear();

    //update to the next turn if the current chip has fallen
    //only run if the game is not won
    if(!wonGame){
        if(chips[turn].Y > TILE_SIZE && chips[turn].GetSpeedY() == 0){

            //Set the tilecolor for the chip just played
            CArea::AreaControl.SetTileColor((int)chips[turn].X, (int)chips[turn].Y, (int)chips[turn].ChipColor);

            //Check to see if game is won enable win graphic if win
            int test = WinGame();
            if(WinGame() !=0){
                CEntity::EntityList.push_back(&win);
                //add to chip back to EntityList
                CEntity::EntityList.push_back(&chips[turn]);
                //update postion
                chips[turn].X = 137;
                chips[turn].Y = 83;
                //Change Color to match winning color
                if(test == 20){
                    chips[turn].SetSubImage(0,0);
                }else{
                    chips[turn].SetSubImage(1,0);
                }
                //update restart string
                testString.str(std::string());
                testString << "Press R to play again: ";
                wonGame = true;
            }

            //do not update if the number of turns exceeds chips
            if(turn < NUMBER_CHIPS-1 && !wonGame){
                //update the turn
                turn++;

                //move to the next chip - BUG: crashes when turn > # of chips
                CEntity::EntityList.push_back(&chips[turn]);

                //turn the arrow back on
                arrow.RenderOn = true;

                //place next chip in the same column as previos
                chips[turn].X = chips[turn-1].X;

                //if the chip you just dropped is red, turn the next one black. If not, already red
                if(chips[turn-1].ChipColor == RED){
                    chips[turn].ChipColor = BLACK;
                    chips[turn].SetSubImage(1,0);
                }
            }
        }
    }


}
